package com.ruoyi.system.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.common.core.domain.entity.CompanyContent;
import com.ruoyi.common.core.domain.model.Company;
import com.ruoyi.common.core.domain.model.HtmlContent;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.CompanyUtils;
import com.ruoyi.common.utils.HtmlContentUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.mapper.CompanyContentMapper;
import com.ruoyi.system.service.ICompanyContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;

@Service
public class CompanyContentServiceImpl implements ICompanyContentService {

    @Autowired
    private CompanyContentMapper companyContentMapper;

    @Override
    public Company getCompany() {
        /**
         * 先从redis取，取不到从数据库取
         */
        Company company = CompanyUtils.getCompanyCache();
        if (StringUtils.isNull(company)) {
            CompanyContent companyContent = companyContentMapper.selectCompanyContent().get(0);
            company = JSONObject.parseObject(companyContent.getCompany(), new TypeReference<Company>(){});
            CompanyUtils.setCompanyCache(company);
        }
        return company;
    }

    @Override
    public void setCompany(String fieldName, String value) {
        /**
         * 先更新数据库 再更新缓存
         */
        Company company = getCompany();
        try {
            Field field = Company.class.getDeclaredField(fieldName);
            field.setAccessible(true);
            field.set(company, value);
            CompanyContent companyContent = new CompanyContent();
            companyContent.setId(1L);
            companyContent.setCompany(JSON.toJSONString(company));
            companyContentMapper.updateCompanyContent(companyContent);
            CompanyUtils.setCompanyCache(company);
        } catch (NoSuchFieldException e) {
            throw new ServiceException(StringUtils.format("公司字段{}不存在,设置值失败", fieldName));
        } catch (IllegalAccessException e) {
            throw new ServiceException(StringUtils.format("公司字段{}设置值失败", fieldName));
        }
    }

    @Override
    public HtmlContent getHtmlContent() {
        /**
         * 先从redis取，取不到从数据库取
         */
        HtmlContent htmlContent = HtmlContentUtils.getHtmlContent();
        if (StringUtils.isNull(htmlContent)) {
            CompanyContent companyContent = companyContentMapper.selectCompanyContent().get(0);
            htmlContent = JSONObject.parseObject(companyContent.getHtmlContent(), new TypeReference<HtmlContent>(){});
            HtmlContentUtils.setHtmlContentFieldValue("footerContactContent" , htmlContent.getFooterContactContent());
            HtmlContentUtils.setHtmlContentFieldValue("headerContent" , htmlContent.getHeaderContent());
            HtmlContentUtils.setHtmlContentFieldValue("footerLogoContent" , htmlContent.getFooterLogoContent());
            HtmlContentUtils.setHtmlContentFieldValue("banner" , htmlContent.getBanner());
            HtmlContentUtils.setHtmlContentFieldValue("advantages" , htmlContent.getAdvantages());
            HtmlContentUtils.setHtmlContentFieldValue("saleServices" , htmlContent.getSaleServices());
            HtmlContentUtils.setHtmlContentFieldValue("indexBanners" , htmlContent.getIndexBanners());
            HtmlContentUtils.setHtmlContentFieldValue("headerIcon" , htmlContent.getHeaderIcon());
            HtmlContentUtils.setHtmlContentFieldValue("indexNewsContent", htmlContent.getIndexNewsContent());
            HtmlContentUtils.setHtmlContentFieldValue("indexProductContent", htmlContent.getIndexProductContent());
            HtmlContentUtils.setHtmlContentFieldValue("indexSolutionContent", htmlContent.getIndexSolutionContent());
            HtmlContentUtils.setHtmlContentFieldValue("indexVideoContent", htmlContent.getIndexVideoContent());
        }
        return htmlContent;
    }

    @Override
    public void updateHtmlContent(HtmlContent htmlContent) {
        HtmlContent content = getHtmlContent();
        if (!StringUtils.isEmpty(htmlContent.getHeaderContent())) {
            content.setHeaderContent(htmlContent.getHeaderContent());
            HtmlContentUtils.setHtmlContentFieldValue("headerContent" , htmlContent.getHeaderContent());
        }
        if (!StringUtils.isEmpty(htmlContent.getFooterContactContent())) {
            content.setFooterContactContent(htmlContent.getFooterContactContent());
            HtmlContentUtils.setHtmlContentFieldValue("footerContactContent" , htmlContent.getFooterContactContent());
        }
        if (!StringUtils.isEmpty(htmlContent.getAdvantages())) {
            content.setAdvantages(htmlContent.getAdvantages());
            HtmlContentUtils.setHtmlContentFieldValue("advantages" , htmlContent.getAdvantages());
        }
        if (!StringUtils.isEmpty(htmlContent.getSaleServices())) {
            content.setSaleServices(htmlContent.getSaleServices());
            HtmlContentUtils.setHtmlContentFieldValue("saleServices" , htmlContent.getSaleServices());
        }
        if (!StringUtils.isEmpty(htmlContent.getFooterLogoContent())) {
            content.setFooterLogoContent(htmlContent.getFooterLogoContent());
            HtmlContentUtils.setHtmlContentFieldValue("footerLogoContent" , htmlContent.getFooterLogoContent());
        }
        if (!StringUtils.isEmpty(htmlContent.getBanner())) {
            content.setBanner(htmlContent.getBanner());
            HtmlContentUtils.setHtmlContentFieldValue("banner" , htmlContent.getBanner());
        }
        if (!StringUtils.isEmpty(htmlContent.getIndexBanners())) {
            content.setIndexBanners(htmlContent.getIndexBanners());
            HtmlContentUtils.setHtmlContentFieldValue("indexBanners" , htmlContent.getIndexBanners());
        }
        if (!StringUtils.isEmpty(htmlContent.getHeaderIcon())) {
            content.setHeaderContent(htmlContent.getHeaderIcon());
            HtmlContentUtils.setHtmlContentFieldValue("headerIcon" , htmlContent.getHeaderIcon());
        }
        if (!StringUtils.isEmpty(htmlContent.getIndexNewsContent())) {
            content.setIndexNewsContent(htmlContent.getIndexNewsContent());
            HtmlContentUtils.setHtmlContentFieldValue("indexNewsContent" , htmlContent.getIndexNewsContent());
        }
        if (!StringUtils.isEmpty(htmlContent.getIndexProductContent())) {
            content.setIndexProductContent(htmlContent.getIndexProductContent());
            HtmlContentUtils.setHtmlContentFieldValue("indexProductContent" , htmlContent.getIndexProductContent());
        }
        if (!StringUtils.isEmpty(htmlContent.getIndexSolutionContent())) {
            content.setIndexSolutionContent(htmlContent.getIndexSolutionContent());
            HtmlContentUtils.setHtmlContentFieldValue("indexSolutionContent" , htmlContent.getIndexSolutionContent());
        }
        if (!StringUtils.isEmpty(htmlContent.getIndexVideoContent())) {
            content.setIndexVideoContent(htmlContent.getIndexVideoContent());
            HtmlContentUtils.setHtmlContentFieldValue("indexVideoContent" , htmlContent.getIndexVideoContent());
        }
        CompanyContent companyContent = new CompanyContent();
        companyContent.setId(1L);
        companyContent.setHtmlContent(JSON.toJSONString(content));
        companyContentMapper.updateCompanyContent(companyContent);
    }

    @Override
    public Object getHtmlContentValueByField(String fieldName) {
        Object value = HtmlContentUtils.getHtmlContentValueByField(fieldName);
        if (StringUtils.isNull(value)) {
            CompanyContent companyContent = companyContentMapper.selectCompanyContent().get(0);
            HtmlContent htmlContent = JSONObject.parseObject(companyContent.getHtmlContent(), new TypeReference<HtmlContent>(){});
            try {
                Field field = HtmlContent.class.getDeclaredField(fieldName);
                field.setAccessible(true);
                value = field.get(htmlContent);
            } catch (NoSuchFieldException e) {
                throw new ServiceException(StringUtils.format("字段{}不存在,获取值失败", fieldName));
            } catch (IllegalAccessException e) {
                throw new ServiceException(StringUtils.format("字段{}获取值失败", fieldName));
            }
        }
        return value;
    }

    @Override
    public Company resetCompany() {
        CompanyContent companyContent = companyContentMapper.selectCompanyContent().get(0);
        Company company = JSONObject.parseObject(companyContent.getCompany(), new TypeReference<Company>(){});
        CompanyUtils.setCompanyCache(company);
        return company;
    }

    @Override
    public HtmlContent resetHtmlContent() {
        CompanyContent companyContent = companyContentMapper.selectCompanyContent().get(0);
        HtmlContent htmlContent = JSONObject.parseObject(companyContent.getHtmlContent(), new TypeReference<HtmlContent>(){});
        HtmlContentUtils.setHtmlContentFieldValue("footerContactContent" , htmlContent.getFooterContactContent());
        HtmlContentUtils.setHtmlContentFieldValue("headerContent" , htmlContent.getHeaderContent());
        HtmlContentUtils.setHtmlContentFieldValue("footerLogoContent" , htmlContent.getFooterLogoContent());
        HtmlContentUtils.setHtmlContentFieldValue("banner" , htmlContent.getBanner());
        HtmlContentUtils.setHtmlContentFieldValue("advantages" , htmlContent.getAdvantages());
        HtmlContentUtils.setHtmlContentFieldValue("saleServices" , htmlContent.getSaleServices());
        HtmlContentUtils.setHtmlContentFieldValue("indexBanners" , htmlContent.getIndexBanners());
        HtmlContentUtils.setHtmlContentFieldValue("headerIcon" , htmlContent.getHeaderIcon());
        HtmlContentUtils.setHtmlContentFieldValue("indexNewsContent", htmlContent.getIndexNewsContent());
        HtmlContentUtils.setHtmlContentFieldValue("indexProductContent", htmlContent.getIndexProductContent());
        HtmlContentUtils.setHtmlContentFieldValue("indexSolutionContent", htmlContent.getIndexSolutionContent());
        HtmlContentUtils.setHtmlContentFieldValue("indexVideoContent", htmlContent.getIndexVideoContent());
        return htmlContent;
    }
}
